package com.centling.smartSealForPhone.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.bean.StampListBean;
import com.centling.smartSealForPhone.constants.SPConstants;
import com.centling.smartSealForPhone.constants.UserType;
import com.centling.smartSealForPhone.utils.ImageUtil;
import com.centling.smartSealForPhone.utils.SPUtil;
import com.centling.smartSealForPhone.view.CircleImageView;

import java.util.List;
import java.util.Locale;

/**
 * @author fionera
 */
public class RvStampListAdapter
        extends RecyclerView.Adapter<RvStampListAdapter.StampListViewHolder> {
    private Context context;
    private List<StampListBean.RowsEntity> list;
    private OnItemClickListener onItemClickListener;
    private OnButtonClickListener onButtonClickListener;

    private boolean needShowApplySn;

    public RvStampListAdapter(Context context, List<StampListBean.RowsEntity> list) {
        this.context = context;
        this.list = list;

        needShowApplySn = TextUtils.equals("1", SPUtil.getString(SPConstants.ENABLE_APP_PULL));
    }

    @NonNull
    @Override
    public StampListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new StampListViewHolder(
                LayoutInflater.from(context).inflate(R.layout.rv_stamp_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final StampListViewHolder holder, final int position) {
        ImageUtil.loadAvatarImage(list.get(position).getApplicantUserAvatar(),
                holder.civHead, R.drawable.iv_avatar_default);
        holder.tvName.setText(list.get(position).getApplicantUserName());
        holder.tvReasonTitle.setText(
                TextUtils.equals(list.get(position).getApplyType(), "COUNT_LIMIT") ? String
                        .format(Locale.CHINA, "可用次数:%s/%s次", list.get(position).getRemainTimes(),
                                list.get(position).getApplyTimes()) : String
                        .format(Locale.CHINA, "%s至%s", list.get(position).getApplyStartTimeReal().split(" ")[0],
                                list.get(position).getApplyEndTimeReal().split(" ")[0]));
        holder.tvReason.setText(list.get(position).getApplyReasonType().getName());
        holder.tvApplySn.setText(needShowApplySn ? String.format(Locale.CHINA, "流程码:%s", list.get(
                position).getApplySn()) : "");
        holder.tvTime.setText(
                String.format(Locale.CHINA, "申请时间:%s", list.get(position).getApplyDate()));
        holder.tvTime.setVisibility(View.GONE);
        if (TextUtils.equals(list.get(position).getEmbedEquipOutFlag(), "1")) {
            holder.tvStamp.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onButtonClickListener.onButtonClick(v, position, 0);
                    }
                });
            holder.tvStamp.setText("用印");
            holder.tvStamp.setTextColor(ContextCompat.getColor(context, R.color.blue1));
            holder.tvStamp.setBackgroundResource(R.drawable.sl_bg_white_blue1_white_blue2_corner4);
        } else {
            holder.tvStamp.setOnClickListener(null);
            holder.tvStamp.setText("审批通过");
            holder.tvStamp.setTextColor(ContextCompat.getColor(context, R.color.text_default));
            holder.tvStamp.setBackground(null);
        }

        if (TextUtils.equals(list.get(position).getApplyCreatorTypeFlag(),
                UserType.EQUIP_COMMON_USER) && !TextUtils.equals(SPUtil.getString(SPConstants.USER_TYPE),
                UserType.EQUIP_COMMON_USER)) {
            holder.tvStamp.setOnClickListener(null);
            holder.tvStamp.setText("审批通过");
            holder.tvStamp.setTextColor(ContextCompat.getColor(context, R.color.text_default));
            holder.tvStamp.setBackground(null);
        }

        holder.tvStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onButtonClickListener.onButtonClick(v, position, 1);
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(holder.itemView, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int pos);
    }

    public interface OnButtonClickListener {
        void onButtonClick(View view, int pos, int type);
    }

    class StampListViewHolder
            extends RecyclerView.ViewHolder {
        CircleImageView civHead;
        TextView tvName, tvTime, tvReasonTitle, tvReason, tvApplySn, tvStamp, tvStop;

        StampListViewHolder(View itemView) {
            super(itemView);
            civHead = itemView.findViewById(R.id.civ_stamp_list_preview);
            tvName = itemView.findViewById(R.id.tv_stamp_list_name);
            tvReasonTitle = itemView.findViewById(R.id.tv_stamp_list_reason_title);
            tvReason = itemView.findViewById(R.id.tv_stamp_list_reason);
            tvApplySn = itemView.findViewById(R.id.tv_stamp_list_apply_sn);
            tvTime = itemView.findViewById(R.id.tv_stamp_list_time);
            tvStamp = itemView.findViewById(R.id.tv_stamp_list_stamp);
            tvStop = itemView.findViewById(R.id.tv_stamp_list_stop);
        }
    }
}
